package com.tarzan.system.service.impl;

import com.tarzan.api.model.response.user.SysUserDetailInfoResp;
import com.tarzan.common.core.config.ConfigUtils;
import com.tarzan.common.core.exception.BusinessException;
import com.tarzan.common.core.exception.ErrorCodesEnum;
import com.tarzan.security.model.LoginUser;
import com.tarzan.system.service.ISysMenuService;
import com.tarzan.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Package com.tarzan.oauth.service.impl
 * @Title UserDetailsServiceImpl
 * @Description TODO
 * @Author liujinshan
 * @CreateTime 2021/6/29 11:03
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    private static final Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);

    @Autowired
    private ISysMenuService iSysMenuService;

    @Autowired
    private ISysUserService iSysUserService;


    /**
     * Locates the user based on the username. In the actual implementation, the search
     * may possibly be case sensitive, or case insensitive depending on how the
     * implementation instance is configured. In this case, the <code>UserDetails</code>
     * object that comes back may have a username that is of a different case than what
     * was actually requested..
     *
     * @param username the username identifying the user whose data is required.
     * @return a fully populated user record (never <code>null</code>)
     * @throws UsernameNotFoundException if the user could not be found or the user has no
     *                                   GrantedAuthority
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUserDetailInfoResp sysUserInfoResp = iSysUserService.findUserInfoByUserName(username);
        if (null == sysUserInfoResp) {
            log.info("登录用户：{} 用户不存在.", username);
            throw new UsernameNotFoundException("用户不存在");
        }
        else if (ConfigUtils.USER_START.equals(sysUserInfoResp.getStatus()))
        {
            log.info("登录用户：{} 已被停用.", username);
            throw new BusinessException(ErrorCodesEnum.ACCOUNT_DEACTIVATED_ERROR.code(),ErrorCodesEnum.ACCOUNT_DEACTIVATED_ERROR.msg());
        }
        UserDetails userDetails;
        userDetails = getUserDetails(sysUserInfoResp);
        return userDetails;
    }


    private UserDetails getUserDetails(SysUserDetailInfoResp sysUserInfoResp) {
        List<String> permissions = iSysMenuService.selectMenuPermsByUserId(sysUserInfoResp.getId());
        return new LoginUser(sysUserInfoResp.getUserName(),sysUserInfoResp.getPassword(),permissions);
    }
}
